package extensions.prototipador.sprites;

import java.io.IOException;

import org.xmlpull.v1.XmlSerializer;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;

public class Sprite {
	
	private static Paint defaultPaint = new Paint();
	
	static{
		defaultPaint.setColor(Color.WHITE);
	}

	protected float _x;
	protected float _y;
	protected int _width;
	protected int _height;
	
	public Sprite(float x, float y)
	{
		_x = x;
		_y = y;
	}
	
	public void tick(long dt)
	{
	}

	public float getX()
	{
		return _x;
	}
	
	public float getY()
	{
		return _y;
	}
	
	public void setPosition(float x, float y)
	{
		_x = x;
		_y = y;
	}
	
	public boolean contains(int x, int y)
	{
		return false;
	}
	
	public void doDraw(Canvas canvas)
	{
	}
	
	public void doDetailDraw(Canvas canvas)
	{
		canvas.drawText("("+(int)_x+", "+(int)_y+")["+_width+", "+_height+"]", _x-5, _y-5, defaultPaint);
	}

	public void move(int i, int j) {
		_x += i;
		_y += j;
	}

	public void move(float x, float y) {
		move((int)x, (int)y);
	}

	/**
	 * Sets an attribute of the Sprite..
	 * 
	 * @param key
	 * @param value
	 */
	public void setAttribute(String key, String value)
	{
		if (key.equalsIgnoreCase("x"))
		{
			_x = Float.parseFloat(value);
		}
		else if (key.equalsIgnoreCase("y"))
		{
			_y = Float.parseFloat(value);
		}else if (key.equalsIgnoreCase("w"))
			_width = Integer.parseInt(value);
		else if (key.equalsIgnoreCase("h"))
			_height = Integer.parseInt(value);
	}

	public void saveToFile(XmlSerializer parser) throws IllegalArgumentException, IllegalStateException, IOException {
		
		parser.attribute(null, "x", _x+"");
		parser.attribute(null, "y", _y+"");
		parser.attribute(null, "w", _width+"");
		parser.attribute(null, "h", _height+"");
	}

	public int getWidth()
	{
		return _width;
	}
	
	public int getHeight()
	{
		return _height;
	}
}
